/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testonlineconnectdb;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author AngelKid
 */
public class SaxProcess extends DefaultHandler{
    boolean flagFound,flagUpdate;
    String numQ;
    int codeFound = 0;
    Question question;
    Question result;
    int choice;
    boolean bCauHoi, bMaCauHoi,bDoKho,bDapAn,flagChoice,bNoiDung;
    public SaxProcess(String index)
    {
        flagUpdate = flagFound = bCauHoi = bMaCauHoi = bDoKho = bDapAn = flagChoice = bNoiDung = false;
        numQ = index;
    }
    public Question GetQuestion()
    {
        if(flagFound)
            return result;
        else
            return null;
    }
    @Override
    public void startElement(String uri,String localName,String qName, Attributes attributes){
        if(qName.equals("CauHoi"))
        {
            question = new Question();
            choice = 0;
            bCauHoi = true;
        }
        if(qName.equals("NoiDung"))
        {
            bNoiDung = true;
        }
        if(qName.equals("LuaChon"))
        {
            choice++;
            flagChoice = true;
        }
        if(qName.equals("DapAn"))
        {
            bDapAn = true;
        }
        if(qName.equals("DoKho"))
        {
            bDoKho = true;
        }
        if(qName.equals("MaCauHoi"))
        {
            bMaCauHoi = true;
        }
        
    }
    @Override
    public void characters(char[] ch, int start, int length)
    {
        String str = new String(ch,start,length);
        if(bNoiDung)
        {
            question.ask = str;
        }
        if(bDapAn)
        {
            question.result = str;
        }
        if(bDoKho)
        {
            question.testType = str;
        }
        if(bMaCauHoi)
        {
            question.code = str;
        }
        if(flagChoice)
        {
            switch(choice)
            {
                case 1: question.choice1 = str; break;
                case 2: question.choice2 = str; break;
                case 3: question.choice3 = str; break;
                case 4: question.choice4 = str; break;    
                default:
            }
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName){
        if(qName.equals("CauHoi"))
        {
            bCauHoi = false;
            if(flagUpdate)
            {
                result = new Question(question);                
                flagUpdate = false;
            }
        }
        if(qName.equals("NoiDung"))
        {
            bNoiDung = false;
        }
        if(qName.equals("LuaChon"))
        {
            flagChoice = false;
        }
        if(qName.equals("DapAn"))
        {
            bDapAn = false;
        }
        if(qName.equals("DoKho"))
        {
            bDoKho = false;
        }
        if(qName.equals("MaCauHoi"))
        {
            bMaCauHoi = false;
            if(question.code.equals(numQ) == true)
            {
                flagFound = flagUpdate = true;
            }
        }
    }
    
}
